/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.component;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.OnPageItemClickListener;
import com.to.aboomy.bannersample.pagetransformer.ScalePageTransformer;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator2;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;

/**
 * ViewPage嵌套自定义组合控件
 *
 * @since 2021-07-29
 */
public class ItemPagerNested extends DirectionalLayout {
    private static final int NUM_3 = 3;
    private static final int NUM_37 = 37;
    private static final int NUM_88 = 88;
    private static final int NUM_3000 = 3000;

    /**
     * 构造
     *
     * @param context Context
     */
    public ItemPagerNested(Context context) {
        this(context,null);
    }

    /**
     * 构造
     *
     * @param context Context
     * @param attrSet AttrSet
     */
    public ItemPagerNested(Context context, AttrSet attrSet) {
        this(context, attrSet,null);
    }

    /**
     * 构造
     *
     * @param context Context
     * @param attrSet AttrSet
     * @param styleName String
     */
    public ItemPagerNested(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        Component rootView = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_item_pager_nested, this, true);
        Banner banner = (Banner) rootView.findComponentById(ResourceTable.Id_banner);
        Banner banner1 = (Banner) rootView.findComponentById(ResourceTable.Id_banner1);

        banner.setHolderCreator(new ImageHolderCreator2())
                .setAutoTurningTime(NUM_3000)
                .setPageMargin(-(AttrHelper.vp2px(NUM_88,getContext())),AttrHelper.vp2px(NUM_37,getContext()))
                .setPageTransformer(true, new ScalePageTransformer())
                .setOnPageClickListener(new OnPageItemClickListener() {
                    @Override
                    public void onPageItemClick(int position) {
                        ToastUtil.showToast(context,String.valueOf(position));
                    }
                })
                .setPages(Utils.getImage(NUM_3));

        // 自定播放
        banner.setAutoPlay(true);
        banner.setOnPageClickListener(position -> {
            showToast(position + "");
        });

        banner1.setHolderCreator(new ImageHolderCreator2())
                .setAutoTurningTime(NUM_3000)
                .setPageMargin(0, 0)
                .setOnPageClickListener(new OnPageItemClickListener() {
                    @Override
                    public void onPageItemClick(int position) {
                        ToastUtil.showToast(context, String.valueOf(position));
                    }
                })
                .setPages(Utils.getImage(NUM_3));

        // 自定播放
        banner1.setAutoPlay(true);
        banner1.setOnPageClickListener(position -> {
            showToast(position + "");
        });
    }

    /**
     * showToast
     *
     * @param toastValue String
     */
    private void showToast(String toastValue) {
        ToastUtil.showToast(mContext, toastValue);
    }
}
